package com.yl.zqy.controller.sys;

import com.yl.zqy.common.CustomException;
import com.yl.zqy.common.Result;
import com.yl.zqy.dto.SysDeptDTO;
import com.yl.zqy.entity.sys.SysDept;
import com.yl.zqy.enums.ResultCode;
import com.yl.zqy.service.sys.SysDeptService;
import com.yl.zqy.service.sys.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author liang.yu
 * @since 2024-03-24
 */
@RestController
@RequestMapping("/sysDept")
@Tag(name = "部门管理", description = "SysDeptController")
@RequiredArgsConstructor
public class SysDeptController {
    private final SysDeptService deptService;
    private final SysUserService userService;

    @PostMapping("/add")
    @Operation(summary = "新增部门信息", tags = "部门管理")
    public Result<Boolean> insertDept(@RequestBody SysDept dept) {
        Optional.ofNullable(dept.getName()).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "部门名称不能为空！"));
        return deptService.insertDept(dept);
    }

    @PutMapping("/update")
    @Operation(summary = "修改部门信息", tags = "部门管理")
    public Result<Boolean> updateDept(@RequestBody SysDept dept) {
        Optional.ofNullable(dept.getId()).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "部门ID不能为空！"));
        Optional.ofNullable(dept.getName()).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "部门名称不能为空！"));
        //上级部门不能为自身
        Optional.ofNullable(dept.getPid()).filter(v -> v.equals(dept.getId())).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "上级部门不能为自身！"));

        //上级部门不能为下级部门
        List<String> subDeptList = deptService.getSubDeptIdList(dept.getId());
        if (subDeptList.contains(dept.getPid())) {
            throw new CustomException(ResultCode.PARAMS_ERROR, "上级部门不能为下级部门!");
        }
        return deptService.updateDpet(dept);
    }

    @DeleteMapping("/deleteById/{id}")
    @Operation(summary = "删除部门信息", tags = "部门管理")
    public Result<Boolean> deleteById(@Parameter(description = "id") @PathVariable(value = "id") String id) {
        Optional.ofNullable(id).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "部门ID不能为空！"));
        //判断是否有子部门
        List<String> subList = deptService.getSubDeptIdList(id);
        Optional.ofNullable(subList).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "当前部门下有子部门，不能删除！"));

        //判断部门下面是否有用户
        int count = userService.getCountByDeptId(id);
        if (count > 0) {
            throw new CustomException(ResultCode.PARAMS_ERROR, "当前部门下面存在有用户，不能删除！");
        }
        return deptService.deleteById(id);
    }

    @GetMapping("/deptList")
    @Operation(summary = "部门信息列表", tags = "部门管理")
    public Result<List<SysDeptDTO>> deptList(
            @Parameter(description = "部门名称") @RequestParam(value = "name", required = false) String name) {
        return Result.ok(deptService.deptList(name));
    }

    @GetMapping("/deptSelectTree")
    @Operation(summary = "部门菜单下拉树", tags = "部门管理")
    public Result<List<SysDeptDTO>> deptSelectTree() {
        return Result.ok(deptService.deptSelectTree());
    }

}
